<?php

namespace App\Repositories;

use App\Models\RechargeCombo;
use App\Models\Member;
use App\Models\Order;

use Exception;

use Illuminate\Support\Facades\DB;

class RechargeComboRepository
{
    /*
     * 根据用户身份获取对应的充值赠送金额
     */
    public function get_recharge_prices($uid, $from = "")
    {

        $member = Member::where("id", "=", $uid)->first(['gender', 'current_status']);
        if (!$member) return "";
        if (is_str_has($member['current_status'], '大')) {
            $type = 1;
        } else if (is_str_has($member['current_status'], '研')) {
            $type = 2;
        } else if (is_str_has($member['current_status'], '博')) {
            $type = 3;
        } else if (is_str_has($member['current_status'], '工')) {
            $type = 4;
        } else {
            $type = 2;
        }

        if ($member['gender'] == 'F') {
            $sex = 0;
        } elseif ($member['gender'] == 'M') {
            $sex = 1;
        }

        // $data['from'] = $from;
//        $data['sex'] = $sex;
//        $data['type'] = $type;
        $data['status'] = 1;//数据有效
        $recharge_prices = RechargeCombo::where($data)
                            ->orderBy('amount', 'asc')
                            ->get(['id', 'amount', 'price', 'discount',"market_price"])
                            ->toArray();

        $where['uid'] = $uid;//用户
        $where['status'] = 40;//成功支付
        $arr = Order::query()
            ->select(
                'total_price',
                DB::raw('COUNT(total_price) as count')
            )
            ->where($where)
            ->whereIn("type", [7, 8])
            ->groupBy('total_price')
            ->pluck('count', 'total_price');

        $rank1 = 0;
        $rank2 = 0;
        foreach ($arr as $k => $v) {
            if ($k < 20) {
                $rank1 = $rank1 + $v;
            } elseif ($k >= 20 && $k < 57) {
                $rank2 = $rank2 + $v;
            }
        }
        $rank1_flag = false;
        if ($rank1 >= 2) {
            $rank1_flag = true;
            //删除数组第一个元素
            unset($recharge_prices[0]);
            $recharge_prices = array_values($recharge_prices);
        } else {
            //删除数组最后一个元素
            array_pop($recharge_prices);
        }

        if ($rank2 >= 1 && $rank1_flag) {
            //删除数组第一个元素
            unset($recharge_prices[0]);
            $recharge_prices = array_values($recharge_prices);
        } elseif ($rank2 >= 1 && !$rank1_flag) {
            //删除数组第一个元素
            unset($recharge_prices[1]);
            $recharge_prices = array_values($recharge_prices);
        } else {
            //删除数组最后一个元素
            array_pop($recharge_prices);
        }

        return $recharge_prices;
    }

    /*
    * 获取当前充值+返现的总金额
    * $uid UID
    * $price 选择充值的金额
    */
    function get_recharge_total_amount($id)
    {
        $data = RechargeCombo::where("id", "=", $id)->first();
//        return $data['amount'] + $data['give'];
        return $data['amount'];
    }
}

